package edu.zut.software.identity.Service.impl;

import edu.zut.software.identity.Repository.TeaMajorRepository;
import edu.zut.software.identity.Service.TeaMajorService;
import edu.zut.software.identity.domain.StuMajor;
import edu.zut.software.identity.domain.TeaMajor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class TeaMajorServiceImpl implements TeaMajorService {
    private TeaMajorRepository teaMajorRepository;
    @Autowired
    public void setTeaMajorRepository(TeaMajorRepository teaMajorRepository) {
        this.teaMajorRepository = teaMajorRepository;
    }

    @Override
    public TeaMajor findById(String id) {
        return teaMajorRepository.findById(id).orElseThrow(RuntimeException::new);
    }

    @Override
    public List<TeaMajor> findAll() {
        return teaMajorRepository.findAll();
    }

    @Override
    public void deleteById(String id) {
        teaMajorRepository.deleteById(id);
    }

    @Override
    public void save(TeaMajor teaMajor) {
        teaMajorRepository.save(teaMajor);
    }

    @Override
    public boolean isExists(String id) {
        return teaMajorRepository.existsById(id);
    }

    @Override
    public int getId(String id) {
        return teaMajorRepository.findById(id).orElseThrow(RuntimeException::new).getWxId();
    }

    @Override
    public Page<TeaMajor> findAll(Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page, pageSize);
        return teaMajorRepository.findAll(pageable);
    }

    @Override
    public List<TeaMajor> findAllByCollegeId(String collegeId) {
        List<TeaMajor> majorLi=new ArrayList<>();
        List<TeaMajor> teaMajors = teaMajorRepository.findAll();
        for (TeaMajor teaMajor : teaMajors) {
            if(Objects.equals(teaMajor.getCollegeId(), collegeId)){
                majorLi.add(teaMajor);
            }
        }
        return majorLi;
    }

}
